#include "string.h"


ykh::string::string(const char* str)
	:_size(strlen(str))
{
	_capacity = _size;
	_str = new char[_capacity + 1];
	strcpy(_str, str);
}

ykh::string::string(const string& str)
	:_size(str._size)
	, _capacity(str._capacity)
{
	_str = new char[_capacity + 1];
	strcpy(_str, str._str);
}

ykh::string& ykh::string::operator=(const string& str)
{
	if (this != &str)
	{
		char* temp = new char[str._capacity + 1];
		strcpy(temp, str._str);
		delete[] _str;
		_str = temp;

		_size = str._size;
		_capacity = str._capacity;
	}

	return *this;
}

ykh::string::~string()
{
	delete[] _str;
	_str = nullptr;
	_size = _capacity = 0;
}


ykh::string::iterator ykh::string::begin()
{
	return _str;
}

ykh::string::iterator ykh::string::end()
{
	return _str + _size;
}

ykh::string::const_iterator ykh::string::begin() const
{
	return _str;
}

ykh::string::const_iterator ykh::string::end() const
{
	return _str + _size;
}


bool ykh::string::operator>(const string& str) const
{
	return strcmp(_str, str._str) > 0;
}

bool ykh::string::operator>=(const string& str) const
{
	return *this > str || *this == str;
}

bool ykh::string::operator==(const string& str) const
{
	return strcmp(_str, str._str) == 0;
}

bool ykh::string::operator!=(const string& str) const
{
	return !(*this == str);
}

bool ykh::string::operator<(const string& str) const
{
	return !(*this >= str);
}

bool ykh::string::operator<=(const string& str) const
{
	return !(*this > str);
}

char& ykh::string::operator[](size_t pos)
{
	assert(pos < _size);
	return _str[pos];
}

const char& ykh::string::operator[](size_t pos) const
{
	assert(pos < _size);
	return _str[pos];
}


void ykh::string::push_back(char ch)
{
	if (_size + 1 > _capacity)
	{
		reserve(_capacity * 2);
	}
	_str[_size] = ch;
	_str[++_size] = '\0';
}

void ykh::string::append(const char* str)
{
	size_t len = strlen(str);
	if (_size + len > _capacity)
	{
		reserve(_size + len);
	}
	strcpy(_str + _size, str);
	_size += len;
}

ykh::string& ykh::string::operator+=(const char ch)
{
	push_back(ch);
	return *this;
}

ykh::string& ykh::string::operator+=(const char* str)
{
	append(str);
	return *this;
}

void ykh::string::clear()
{
	_size = 0;
	_str[_size] = '\0';
}

void ykh::string::swap(string& str)
{
	std::swap(_str, str._str);
	std::swap(_size, str._size);
	std::swap(_capacity, str._capacity);
}

const char* ykh::string::c_str()
{
	return _str;
}


size_t ykh::string::size() const
{
	return _size;
}

size_t ykh::string::capacity()const
{
	return _capacity;
}

bool ykh::string::empty()const
{
	return _size == 0;
}

void ykh::string::reserve(size_t n)
{
	if (n > _capacity)
	{
		char* temp = new char[n + 1];
		strcpy(temp, _str);
		delete[] _str;
		_str = temp;

		_capacity = n;
	}
}

void ykh::string::resize(size_t n, char ch)
{
	if (n <= _size)
	{
		_size = n;
		_str[_size] = '\0';
	}
	else
	{
		if (n > _capacity)
		{
			reserve(_capacity * 2);
		}
		size_t i = _size;
		while (i < n)
		{
			_str[i] = ch;
			++i;
		}
		_size = n;
		_str[_size] = '\0';
	}
}


ykh::string& ykh::string::insert(size_t pos, char ch)
{
	assert(pos <= _size);
	if (_size + 1 > _capacity)
	{
		reserve(_capacity * 2);
	}
	size_t end = _size + 1;
	while (end > pos)
	{
		_str[end] = _str[end - 1];
		--end;
	}
	_str[pos] = ch;
	++_size;

	return *this;
}

ykh::string& ykh::string::insert(size_t pos, const char* str)
{
	assert(pos <= _size);
	size_t len = strlen(str);
	if (_size + len > _capacity)
	{
		reserve(_capacity * 2);
	}
	size_t end = _size + len;
	while (end > pos + len - 1)
	{
		_str[end] = _str[end - 1];
		--end;
	}
	strncpy(_str + pos, str, len + 1);
	_size += len;
	return *this;
}

ykh::string& ykh::string::erase(size_t pos, size_t len)
{
	if (len == npos || pos + len >= _size)
	{
		_str[pos] = '\0';
		_size = pos;
	}
	else
	{
		strcpy(_str + pos, _str + pos + len);
		_size -= pos;
	}
	return *this;
}

size_t ykh::string::find(char ch, size_t pos) const
{
	for (size_t i = pos; i < _size; ++i)
	{
		if (_str[i] == ch)
			return i;
	}
	return npos;
}

size_t ykh::string::find(const char* str, size_t pos) const
{
	assert(pos < _size);
	char* p = strstr(_str + pos, str);
	if (p == nullptr)
	{
		return npos;
	}
	else
	{
		return p - _str;
	}
}

ostream& ykh::operator<<(ostream& out, const string& str)
{
	for (auto ch : str)
	{
		out << ch;
	}
	return out;
}

istream& ykh::operator>>(istream& in, string& str)
{
	str.clear();

	char ch = in.get();
	char buff[128];
	size_t i = 0;
	while (ch != '\n')
	{
		buff[i++] = ch;
		if (i == 127)
		{
			buff[127] = '\0';
			str += buff;
			i = 0;
		} 
		ch = in.get();
	}
	if (i != 0)
	{
		buff[i] = '\0';
		str += buff;
	}
	return in;
}

